package com.xuelang.pipeline.app.project.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xuelang.pipeline.app.project.ProjectTypeService;
import com.xuelang.pipeline.app.project.converter.ProjectTypeToDTOConverter;
import com.xuelang.pipeline.client.project.dto.ProjectTypeDTO;
import com.xuelang.pipeline.domain.project.ProjectType;
import com.xuelang.pipeline.domain.project.service.ProjectTypeDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>项目类型服务实现类</p>
 *
 * @author : yixin.lys
 * @date : 2024-09-03 11:31
 **/
@Service
public class ProjectTypeServiceImpl implements ProjectTypeService {

    @Autowired
    private ProjectTypeDomainService projectTypeDomainService;

    @Override
    public Integer createProjectType(ProjectTypeDTO projectTypeDTO) {
        return projectTypeDomainService.createProjectType(ProjectTypeToDTOConverter.INSTANCE.fromDTO(projectTypeDTO));
    }

    @Override
    public boolean deleteProjectTypeById(Integer id) {
        return projectTypeDomainService.deleteProjectTypeById(id);
    }

    @Override
    public boolean updateProjectType(ProjectTypeDTO projectTypeDTO) {
        return projectTypeDomainService.updateProjectType(ProjectTypeToDTOConverter.INSTANCE.fromDTO(projectTypeDTO));
    }

    @Override
    public ProjectTypeDTO findProjectTypeById(Integer id) {
        return ProjectTypeToDTOConverter.INSTANCE.toDTO(projectTypeDomainService.findProjectTypeById(id));
    }

    @Override
    public List<ProjectTypeDTO> findAllProjectTypes() {
        List<ProjectType> allProjectTypes = projectTypeDomainService.findAllProjectTypes();
        if (CollectionUtils.isNotEmpty(allProjectTypes)) {
            return allProjectTypes.stream().map(ProjectTypeToDTOConverter.INSTANCE::toDTO).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public boolean batchUpdateProjectTypes(List<ProjectTypeDTO> projectTypeDTOList) {
        if (CollectionUtils.isNotEmpty(projectTypeDTOList)) {
            return projectTypeDomainService.batchUpdateProjectTypes(
                    projectTypeDTOList.stream().map(ProjectTypeToDTOConverter.INSTANCE::fromDTO).collect(Collectors.toList())
            );
        }
        return true;
    }
}
